package xin.yangda.poiplus.test.service.impl;

import com.google.common.collect.Maps;
import org.assertj.core.util.Lists;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import xin.yangda.poiplus.constants.PoiPlusFileExtend;
import xin.yangda.poiplus.designer.SimpleXlsDesigner;
import xin.yangda.poiplus.designer.csv.CsvDesigner;
import xin.yangda.poiplus.pojo.ExcelData;
import xin.yangda.poiplus.test.entity.UserEntity;
import xin.yangda.poiplus.test.entity.UserGroupEntity;
import xin.yangda.poiplus.test.service.UserService;

import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * @author yangdaxin
 * @version 创建时间 2019/1/17 14:28
 */
@Service
public class UserServiceImpl implements UserService {
    @Override
    public void exportExcelByTemplate(String tempPath, PoiPlusFileExtend poiPlusFileExtend) {
        // 打开模板
        InputStream inputStream = getClass().getClassLoader().getResourceAsStream(tempPath);
        try (SimpleXlsDesigner simpleXlsDesigner = new SimpleXlsDesigner(inputStream)) {
            // 填充数据源
            Map<String, String> map = Maps.newConcurrentMap();
            map.put("createUser", "yangdaxin");
            map.put("createTime", "2019-01-18 18:05:00");

            ExcelData<UserEntity> excelData = new ExcelData<>(UserEntity.class, map, this.queryList());
            simpleXlsDesigner.setData(excelData);

            // 数据加工
            simpleXlsDesigner.process();

            // 保存Excel
            String saveFilePath = "D:\\UserData" + poiPlusFileExtend.getValue();
            simpleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportExcel(PoiPlusFileExtend poiPlusFileExtend) {
        try (SimpleXlsDesigner simpleXlsDesigner = new SimpleXlsDesigner()) {
            // 填充数据源
            ExcelData<UserEntity> excelData = new ExcelData<>(UserEntity.class, this.queryList());
            simpleXlsDesigner.setData(excelData);

            // 数据加工
            simpleXlsDesigner.process();

            // 保存Excel
            String saveFilePath = "D:\\UserData" + poiPlusFileExtend.getValue();
            simpleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportTemplate(PoiPlusFileExtend poiPlusFileExtend) {
        try (SimpleXlsDesigner simpleXlsDesigner = new SimpleXlsDesigner()) {
            // 填充数据源
            ExcelData<UserEntity> excelData = new ExcelData<>(UserEntity.class);
            simpleXlsDesigner.setData(excelData);

            // 模板加工
            simpleXlsDesigner.process(true);

            // 保存Excel
            String saveFilePath = "D:\\UserTemplate" + poiPlusFileExtend.getValue();
            simpleXlsDesigner.saveFile(saveFilePath);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void exportCsv() {
        ExcelData<UserEntity> excelData = new ExcelData<>(UserEntity.class, this.queryList());

        String saveFilePath = "D:\\UserData.csv";
        CsvDesigner.build(excelData).export(saveFilePath);
    }

    @Override
    public List<UserEntity> queryList() {
        DateTime dateTime = new DateTime(1989, 3, 6, 0, 0, 0, 0);
        return Lists.newArrayList(
                UserEntity.builder()
                        .id(1)
                        .name("姓名")
                        .userGroup(UserGroupEntity.builder().id(1).name("研发部").build())
                        .sex(1)
                        .age(29)
                        .birthday(dateTime.toDate())
                        .nativePlace("贵州省")
                        .currentAddress("贵州省贵阳市观山湖区")
                        .build()
        );
    }
}
